﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.ADF.BaseClasses;

namespace ISchool
{
    class ZoomOutORIn : BaseTool
    {
        //作者：郭菲
        //描述：重写拉框放大

        //记录鼠标位置
        private IPoint m_point;
        //标记MouseDown是否发生
        private Boolean m_isMouseDown;
        //追踪鼠标移动产生新的Envelope 
        private INewEnvelopeFeedback m_feedBack;
        IHookHelper m_hookHelper=new HookHelperClass();

        public ZoomOutORIn()
        {
        }
        public override void OnMouseDown(int Button, int Shift, int X, int Y)
        {
            base.OnMouseDown(Button, Shift, X, Y);
            //当前地图视图为空时返回 
            if (m_hookHelper.ActiveView == null)
                return;
            //获取鼠标点击位置 
            m_point = m_hookHelper.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y);
            m_isMouseDown = true;
        }
        public override void OnMouseMove(int Button, int Shift, int X, int Y)
        {
            base.OnMouseMove(Button, Shift, X, Y);
            //MouseDown为发生时返回             
            if (!m_isMouseDown)
                return;
            IActiveView pActiveView = m_hookHelper.ActiveView;
            //m_feedBack追踪鼠标移动             
            if (m_feedBack == null)
            {
                m_feedBack = new NewEnvelopeFeedbackClass();
                //开始追踪 
                m_feedBack.Display = pActiveView.ScreenDisplay;
                m_feedBack.Start(m_point);
            }
            //追踪鼠标移动位置 
            m_feedBack.MoveTo(pActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y));
        }
        public override void OnMouseUp(int Button, int Shift, int X, int Y)
        {
            base.OnMouseUp(Button, Shift, X, Y);
            //MouseDown为发生时返回            
            if (!m_isMouseDown) return;
            IActiveView pActiveView = m_hookHelper.ActiveView;
            //获取MouseUp发生时的范围并放大
            IEnvelope pEnvelope;
            //鼠标未拉框时进行固定比例尺放大
            if (m_feedBack == null)             
            {
                pEnvelope = pActiveView.Extent;
                pEnvelope.Expand(0.5, 0.5, true);
                pEnvelope.CenterAt(m_point);
            }
            else
            {
                //停止追踪 
                pEnvelope = m_feedBack.Stop();
                //判断新的范围的高度和宽度是否为零 
                if (pEnvelope.Width == 0 || pEnvelope.Height == 0)
                {
                    m_feedBack = null;
                    m_isMouseDown = false;
                }
            }
            //获取新的范围 
            pActiveView.Extent = pEnvelope;
            //刷新视图 
            pActiveView.Refresh();
            m_feedBack = null;
            m_isMouseDown = false;
        }
        public override void OnCreate(object hook)
        {
            m_hookHelper.Hook = hook;
        }
    }
}
